/** <a href="http://www.cpupk.com/decompiler">Eclipse Class Decompiler</a> plugin, Copyright (c) 2017 Chen Chao. **/
package com.minierp.service.impl;

import com.minierp.bean.BusinessBillBean;
import com.minierp.bean.BusinessBillItemBean;
import com.minierp.bean.ClieUserBean;
import com.minierp.bean.UserBean;
import com.minierp.dao.AccessoryDao;
import com.minierp.dao.AdjustBillDao;
import com.minierp.dao.BankAccountBalanceDao;
import com.minierp.dao.BankAccountDetailDao;
import com.minierp.dao.BaseDao;
import com.minierp.dao.BusinessBillDao;
import com.minierp.dao.BusinessBillItemDao;
import com.minierp.dao.CooperBillItemDao;
import com.minierp.dao.FeeBillDao;
import com.minierp.dao.FinanceDetailDao;
import com.minierp.dao.IdGenerator;
import com.minierp.dao.OutBillDao;
import com.minierp.dao.OutBillItemDao;
import com.minierp.dao.OutputDao;
import com.minierp.dao.ReceiveBillDao;
import com.minierp.dao.SerialNumberDao;
import com.minierp.dao.StockBalanceDao;
import com.minierp.dao.StockDao;
import com.minierp.err.DataChangedException;
import com.minierp.err.NotAllowException;
import com.minierp.helper.EncCommonHelper;
import com.minierp.helper.PageHelper;
import com.minierp.model.Accessory;
import com.minierp.model.BankAccount;
import com.minierp.model.BusinessBill;
import com.minierp.model.BusinessBillExt;
import com.minierp.model.BusinessBillItem;
import com.minierp.model.BusinessBillItemExt;
import com.minierp.model.BusinessProfit;
import com.minierp.model.BusinessUpdateLog;
import com.minierp.model.CodeRule;
import com.minierp.model.DesignDetail;
import com.minierp.model.EnterpriseInfo;
import com.minierp.model.Establishment;
import com.minierp.model.FinanceDetail;
import com.minierp.model.FollowDetail;
import com.minierp.model.MakeType;
import com.minierp.model.OutBill;
import com.minierp.model.OutBillItem;
import com.minierp.model.OvPercent;
import com.minierp.model.Stock;
import com.minierp.model.SystemLog;
import com.minierp.model.WorkCenter;
import com.minierp.model.WorkProcess;
import com.minierp.service.BusinessBillService;
import java.io.File;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

public class BusinessBillServiceImpl implements BusinessBillService {
	public Map txInsert(BusinessBillBean form, EnterpriseInfo ent, UserBean ub,
			BusinessBillDao businessBillDao,
			BusinessBillItemDao businessBillItemDao,
			SerialNumberDao serialNumberDao, AccessoryDao accessoryDao,
			BankAccountBalanceDao bankAccountBalanceDao,
			CooperBillItemDao cooperBillItemDao, FeeBillDao feeBillDao,
			StockDao stockDao, IdGenerator idGenerator, OutputDao outputDao)
			throws Exception {
		BusinessBill obj = form.getBusinessBill();
		obj.setCreateTime(new Date());
		obj.setId(idGenerator.getId(BusinessBill.class.getName()));
		if (!form.getCodeRule().getGenerateRule().equals("MANUAL")) {
			obj.setBillCode(PageHelper.generateCode(serialNumberDao, form
					.getCodeRule(), "", form.getBusinessBill().getTallySetId()));

			for (BusinessBill be = businessBillDao.get(obj.getBillCode()); be != null; be = businessBillDao
					.get(obj.getBillCode())) {
				obj.setBillCode(PageHelper.generateCode(serialNumberDao, form
						.getCodeRule(), "", form.getBusinessBill()
						.getTallySetId()));
			}
		}

		this.setCost(form, ent.getBusiLengType());
		businessBillDao.save(obj);
		BusinessBillExt arg19 = new BusinessBillExt();
		arg19.setId(obj.getId());

		for (int map = 0; map < form.getItems().size(); ++map) {
			BusinessBillItemBean op = (BusinessBillItemBean) form.getItems()
					.get(map);
			op.setBusinessBillItemExt(new BusinessBillItemExt());
		}

		HashMap arg20 = new HashMap();
		String arg21;
		if (ent.getBusiAutoExamine().equals("Y")) {
			arg19.setExamined("Y");
			arg19.setExamSms("N");
			arg19.setMakeSms("Y");
			arg19.setCollectedCost(obj.getImprest());
			if (obj.getCost().subtract(arg19.getCollectedCost())
					.compareTo(new BigDecimal(0)) == 0) {
				arg19.setCollected("Y");
			}

			arg19.setExamineTime(new Date());
			arg19.setExaminerId(ub.getUser().getStafferId());
			arg19.setExaminerName(ub.getStaffer().getStafferName());
			if (obj.getImprest().compareTo(new BigDecimal(0)) != 0) {
				arg19.setReceTime(obj.getOrderDate());
			}

			arg21 = "Y";

			for (int i = 0; i < form.getItems().size(); ++i) {
				BusinessBillItemBean bean = (BusinessBillItemBean) form
						.getItems().get(i);
				bean.setBusinessBillItemExt(new BusinessBillItemExt());
				if (!bean.getStatus().equals("DELETE")
						&& (bean.getBusinessBillItem().getWcId() != 0L || bean
								.getBusinessBillItem().getIsCoop().equals("Y"))) {
					arg21 = "N";
				}
			}

			arg19.setCompleted(arg21);
			arg20.put("exam", "Y");
			arg20.put("content", obj.getContent());
			arg20.put("billCode", obj.getBillCode());
			arg20.put("phone", obj.getPhone());
			arg20.put("linkman", obj.getLinkman());
		} else {
			arg19.setExamSms("Y");
			arg19.setMakeSms("N");
			arg19.setCollected("N");
			arg19.setExamined("N");
			arg19.setCompleted("N");
		}

		if (arg19.getCompleted().equals("N")) {
			arg19.setCompleteTime((Date) null);
		} else {
			arg19.setCompleteTime(new Date());
		}

		businessBillDao.save(arg19);
		this.saveItems(obj.getId(), "N", "N", form.getItems(),
				businessBillItemDao, outputDao, idGenerator);
		accessoryDao.update(form.getTmpCode(), obj.getId());
		if (arg19.getExamined().equals("Y")
				&& obj.getImprest().compareTo(new BigDecimal(0)) != 0) {
			arg21 = form.getClientName();
			if (obj.getContent() != null && !obj.getContent().equals("")) {
				arg21 = arg21 + "," + obj.getContent();
			}

			PageHelper.updateBankAccount(obj.getBankAccountId(),
					obj.getTallySetId(), obj.getId(), obj.getImprest(),
					obj.getOrderDate(), arg19.getExamineTime(),
					obj.getBillCode(), "BUSINESS_BILL", "", "业务预收", "R",
					obj.getManagerName(), arg21, "BUSINESS", "SAVE_EXAM",
					false, (BankAccountDetailDao) null, bankAccountBalanceDao,
					idGenerator);
		}

		if (arg19.getExamined().equals("Y")) {
			this.updateEstablishment(obj.getClientId(), obj.getOrderDate(),
					obj.getCost(), businessBillDao);
		}

		if (arg19.getExamined().equals("Y")) {
			this.updateProfit(0L, obj.getBillCode(), obj.getCost(),
					arg19.getAdjustCost(), businessBillDao, cooperBillItemDao,
					feeBillDao, stockDao, idGenerator);
		}

		if (arg19.getExamined().equals("Y")) {
			this.insertFinanceDetail(obj, businessBillDao, idGenerator,
					"INSERT");
		}

		this.insertDesignDetail(obj, businessBillDao, idGenerator);
		this.insertFollowDetail(obj, businessBillDao, idGenerator);
		OvPercent arg22 = new OvPercent();
		arg22.setId(idGenerator.getId(OvPercent.class.getName()));
		arg22.setBillId(obj.getId());
		arg22.setBusiOv(ent.getBusiOv());
		arg22.setComDesi(ent.getComDesi());
		arg22.setCliDesi(ent.getCliDesi());
		arg22.setFollOv(ent.getFollOv());
		arg22.setMakeOv(ent.getMakeOv());
		arg22.setReceOv(ent.getReceOv());
		arg22.setSetuOv(ent.getSetuOv());
		arg22.setUpdated("N");
		businessBillDao.save(arg22);
		return arg20;
	}

	public void txInsertForBegiImpo(BusinessBill obj, CodeRule cr,
			BusinessBillItem item, EnterpriseInfo ent, UserBean ub,
			BusinessBillDao businessBillDao, StockDao stockDao,
			SerialNumberDao serialNumberDao, IdGenerator idGenerator)
			throws Exception {
		obj.setCreateTime(new Date());
		obj.setId(idGenerator.getId(BusinessBill.class.getName()));
		if (cr.getGenerateRule().equals("MANUAL")) {
			throw new NotAllowException("业务单编号规则必须设置成自动产生！");
		} else {
			obj.setBillCode(PageHelper.generateCode(serialNumberDao, cr, "",
					obj.getTallySetId()));

			for (BusinessBill be = businessBillDao.get(obj.getBillCode()); be != null; be = businessBillDao
					.get(obj.getBillCode())) {
				obj.setBillCode(PageHelper.generateCode(serialNumberDao, cr,
						"", obj.getTallySetId()));
			}

			businessBillDao.save(obj);
			BusinessBillExt be1 = new BusinessBillExt();
			be1.setId(obj.getId());
			be1.setExamined("Y");
			be1.setExamSms("N");
			be1.setMakeSms("N");
			be1.setExamineTime(new Date());
			be1.setExaminerId(ub.getUser().getStafferId());
			be1.setExaminerName(ub.getStaffer().getStafferName());
			be1.setCompleted("Y");
			be1.setCompleteTime(new Date());
			be1.setDelivered("Y");
			businessBillDao.save(be1);
			item.setId(idGenerator.getId(BusinessBillItem.class.getName()));
			item.setBillId(obj.getId());
			businessBillDao.save(item);
			BusinessBillItemExt bie = new BusinessBillItemExt();
			bie.setId(item.getId());
			businessBillDao.save(bie);
			this.updateEstablishment(obj.getClientId(), obj.getOrderDate(),
					obj.getCost(), businessBillDao);
			this.updateProfit(0L, obj.getBillCode(), obj.getCost(),
					be1.getAdjustCost(), businessBillDao,
					(CooperBillItemDao) null, (FeeBillDao) null, stockDao,
					idGenerator);
			this.insertFinanceDetail(obj, businessBillDao, idGenerator,
					"INSE_BEGI");
			OvPercent op = new OvPercent();
			op.setId(idGenerator.getId(OvPercent.class.getName()));
			op.setBillId(obj.getId());
			op.setBusiOv(ent.getBusiOv());
			op.setComDesi(ent.getComDesi());
			op.setCliDesi(ent.getCliDesi());
			op.setFollOv(ent.getFollOv());
			op.setMakeOv(ent.getMakeOv());
			op.setReceOv(ent.getReceOv());
			op.setSetuOv(ent.getSetuOv());
			op.setUpdated("N");
			businessBillDao.save(op);
		}
	}

	public void txInsertForOnline(BusinessBillBean bean, CodeRule cr,
			EnterpriseInfo ent, ClieUserBean ub,
			BusinessBillDao businessBillDao,
			BusinessBillItemDao businessBillItemDao, AccessoryDao accessoryDao,
			SerialNumberDao serialNumberDao, IdGenerator idGenerator)
			throws Exception {
		this.setCost(bean, ent.getBusiLengType());
		BigDecimal remaCost = businessBillDao.getReceivableCost(ub.getClie()
				.getId(), 0L);
		BigDecimal currCost = (new BigDecimal(ub.getClie().getCreditCost()))
				.subtract(remaCost).subtract(bean.getBusinessBill().getCost());
		if (ub.getUser().getOrdeType().equals("UP_ZERO")
				&& currCost.compareTo(new BigDecimal(0)) < 0) {
			remaCost = (new BigDecimal(0)).subtract(remaCost);
			throw new NotAllowException("您的余额是 " + remaCost + "，不能下单，请联系客服！");
		} else {
			long tsId = ub.getClie().getTallySetId();
			String billCode = "";
			if (cr.getGenerateRule().equals("MANUAL")) {
				throw new NotAllowException("业务单编号规则必须设置成自动产生！");
			} else {
				billCode = PageHelper.generateCode(serialNumberDao, cr, "",
						tsId);

				for (BusinessBill creaName = businessBillDao.get(billCode); creaName != null; creaName = businessBillDao
						.get(billCode)) {
					billCode = PageHelper.generateCode(serialNumberDao, cr, "",
							tsId);
				}

				String arg25 = ub.getClie().getEstName();
				if (arg25.length() > 15) {
					arg25 = arg25.substring(0, 15);
				}

				BusinessBill obj = bean.getBusinessBill();
				obj.setBillCode(billCode);
				obj.setClientId(ub.getUser().getClieId());
				obj.setCreaterName(arg25);
				obj.setCreateTime(new Date());
				obj.setDesignType("CLIENT");
				obj.setId(idGenerator.getId(BusinessBill.class.getName()));
				obj.setLinkman(ub.getClie().getLinkman());
				obj.setManagerId(ub.getClie().getManagerId());
				obj.setManagerName(ub.getClie().getManagerName());
				obj.setPhone(ub.getClie().getLinkmanPhone());
				obj.setSourType("ONLINE");
				obj.setTallySetId(tsId);
				businessBillDao.save(obj);
				BusinessBillExt be = new BusinessBillExt();
				be.setId(obj.getId());
				businessBillDao.save(be);
				int newNum = 0;

				for (int curItemId = 0; curItemId < bean.getItems().size(); ++curItemId) {
					BusinessBillItemBean bib = (BusinessBillItemBean) bean
							.getItems().get(curItemId);
					if (!bib.getStatus().equals("DELETE")) {
						++newNum;
					}
				}

				long arg26 = 0L;
				if (newNum > 0) {
					arg26 = idGenerator.getId(BusinessBillItem.class.getName(),
							newNum);
				}

				for (int op = 0; op < bean.getItems().size(); ++op) {
					BusinessBillItemBean bib1 = (BusinessBillItemBean) bean
							.getItems().get(op);
					BusinessBillItem bi = bib1.getBusinessBillItem();
					bi.setBillId(obj.getId());
					if (!bib1.getStatus().equals("DELETE")) {
						bi.setId(arg26);
						businessBillItemDao.save(bi);
						++arg26;
						BusinessBillItemExt bie = new BusinessBillItemExt();
						bie.setId(bi.getId());
						businessBillItemDao.save(bie);
					}
				}

				accessoryDao.update(bean.getTmpCode(), obj.getId());
				OvPercent arg24 = new OvPercent();
				arg24.setId(idGenerator.getId(OvPercent.class.getName()));
				arg24.setBillId(obj.getId());
				arg24.setBusiOv(ent.getBusiOv());
				arg24.setComDesi(ent.getComDesi());
				arg24.setCliDesi(ent.getCliDesi());
				arg24.setFollOv(ent.getFollOv());
				arg24.setMakeOv(ent.getMakeOv());
				arg24.setReceOv(ent.getReceOv());
				arg24.setSetuOv(ent.getSetuOv());
				arg24.setUpdated("N");
				businessBillDao.save(arg24);
			}
		}
	}

	private void insertDesignDetail(BusinessBill obj,
			BusinessBillDao businessBillDao, IdGenerator idGenerator) {
		if (obj.getDesignerIdStr() != null
				&& !obj.getDesignerIdStr().equals("")) {
			String[] arr = obj.getDesignerIdStr().split(";");
			long curId = idGenerator.getId(DesignDetail.class.getName(),
					arr.length);

			for (int i = 0; i < arr.length; ++i) {
				if (arr[i] != null && !arr[i].equals("")) {
					String[] tmpArr = arr[i].split(":");
					DesignDetail dd = new DesignDetail();
					dd.setId(curId);
					dd.setBillId(obj.getId());
					dd.setPercent(new BigDecimal(tmpArr[1]));
					dd.setStafferId(Long.parseLong(tmpArr[0]));
					businessBillDao.save(dd);
					++curId;
				}
			}
		}

	}

	private void insertFollowDetail(BusinessBill obj,
			BusinessBillDao businessBillDao, IdGenerator idGenerator) {
		if (obj.getFollowerIdStr() != null
				&& !obj.getFollowerIdStr().equals("")) {
			String[] arr = obj.getFollowerIdStr().split(";");
			long curId = idGenerator.getId(FollowDetail.class.getName(),
					arr.length);

			for (int i = 0; i < arr.length; ++i) {
				if (arr[i] != null && !arr[i].equals("")) {
					String[] tmpArr = arr[i].split(":");
					FollowDetail dd = new FollowDetail();
					dd.setId(curId);
					dd.setBillId(obj.getId());
					dd.setPercent(new BigDecimal(tmpArr[1]));
					dd.setStafferId(Long.parseLong(tmpArr[0]));
					businessBillDao.save(dd);
					++curId;
				}
			}
		}

	}

	private void insertFinanceDetail(BusinessBill obj,
			BusinessBillDao businessBillDao, IdGenerator idGenerator,
			String mark) {
		FinanceDetail fd = new FinanceDetail();
		fd.setId(idGenerator.getId(FinanceDetail.class.getName()));
		fd.setBillDate(obj.getOrderDate());
		fd.setBillId(obj.getId());
		fd.setBillType("BUSINESS_BILL");
		fd.setRemCost(obj.getCost().subtract(obj.getImprest()));
		fd.setConCost(obj.getCost());
		fd.setRpCost(obj.getImprest());
		fd.setEstId(obj.getClientId());
		fd.setFinanceType("R");
		fd.setTallySetId(obj.getTallySetId());
		fd.setMark(mark);
		businessBillDao.save(fd);
	}

	private void updateProfit(long billId, String billCode, BigDecimal cost,
			BigDecimal adjustCost, BusinessBillDao businessBillDao,
			CooperBillItemDao cooperBillItemDao, FeeBillDao feeBillDao,
			StockDao stockDao, IdGenerator idGenerator) {
		BusinessProfit p = businessBillDao.getBusinessProfit(billCode);
		BigDecimal cooperCost = new BigDecimal(0);
		BigDecimal feeCost = new BigDecimal(0);
		if (billId != 0L) {
			cooperCost = cooperBillItemDao.getCost(billId);
			feeCost = feeBillDao.getCost(billId);
		}

		BigDecimal materialCost = stockDao.getOutCost(billCode).subtract(
				stockDao.getInCost(billCode));
		BigDecimal profitCost = cost.add(adjustCost).subtract(cooperCost)
				.subtract(feeCost).subtract(materialCost);
		if (p == null) {
			p = new BusinessProfit();
			p.setBbCode(billCode);
			p.setId(idGenerator.getId(BusinessProfit.class.getName()));
			p.setCooperCost(cooperCost);
			p.setFeeCost(feeCost);
			p.setMaterialCost(materialCost);
			p.setProfitCost(profitCost);
			businessBillDao.save(p);
		} else {
			p.setCooperCost(cooperCost);
			p.setFeeCost(feeCost);
			p.setMaterialCost(materialCost);
			p.setProfitCost(profitCost);
			businessBillDao.update(p);
		}

	}

	private void updateEstablishment(long estId, Date busTime, BigDecimal cost,
			BusinessBillDao businessBillDao) {
		Establishment est = (Establishment) businessBillDao.get(
				Establishment.class, estId);
		if (est != null) {
			if (est.getBusTime() == null || est.getBusTime().before(busTime)) {
				est.setBusTime(busTime);
			}

			est.setBusCost(est.getBusCost().add(cost));
			businessBillDao.update(est);
		}

	}

	public void txUpdate(BusinessBillBean form, EnterpriseInfo ent,
			UserBean ub, String editType, List proList,
			BusinessBillDao businessBillDao,
			BusinessBillItemDao businessBillItemDao,
			ReceiveBillDao receiveBillDao, AdjustBillDao adjustBillDao,
			BankAccountBalanceDao bankAccountBalanceDao,
			BankAccountDetailDao bankAccountDetailDao,
			CooperBillItemDao cooperBillItemDao, FeeBillDao feeBillDao,
			StockDao stockDao, IdGenerator idGenerator,
			FinanceDetailDao financeDetailDao, OutputDao outputDao)
			throws Exception {
		BusinessBill obj = form.getBusinessBill();
		Object[] sv = businessBillDao.getStatusValues(obj.getId());
		if (sv == null) {
			throw new Exception("Can\'t get status values for business bill:"
					+ obj.getId());
		} else {
			long existSycVal = ((Date) sv[0]).getTime();
			Date oldDate = (Date) sv[1];
			long oldClientId = Long.parseLong(sv[2].toString());
			long oldBaId = Long.parseLong(sv[3].toString());
			BigDecimal oldImprest = (BigDecimal) sv[4];
			BigDecimal oldFavCost = (BigDecimal) sv[5];
			BigDecimal oldCost = (BigDecimal) sv[6];
			String oldBillCode = (String) sv[7];
			String oldDesignIdStr = (String) sv[8];
			String oldFollowIdStr = (String) sv[9];
			Date oldDelDate = (Date) sv[10];
			BusinessBillExt be = (BusinessBillExt) businessBillDao.get(
					BusinessBillExt.class, obj.getId());
			if (existSycVal != form.getSychronizeValue()) {
				throw new DataChangedException("业务单已被其他操作修改，请关闭页面再打开后重新操作！");
			} else if (be.getEnabled().equals("N")) {
				throw new NotAllowException("业务单已作废，不能修改！");
			} else if (be.getClosed().equals("Y")) {
				throw new NotAllowException("业务单已结清，不能修改！");
			} else if ((editType == null || editType.equals(""))
					&& be.getExamined().equals("Y")) {
				throw new NotAllowException("业务单已审核，不能修改！");
			} else if (ent.getBusiModi().equals("N")
					&& be.getHasReceive().equals("Y")) {
				throw new NotAllowException("业务单已收款，不能修改！");
			} else if (be.getExamined().equals("Y")
					&& oldClientId != form.getBusinessBill().getClientId()) {
				throw new NotAllowException("业务单已审核，不能修改客户！");
			} else {
				List statList = businessBillItemDao.getStatus(obj.getId());

				int newDesignIdStr;
				BusinessBillItemBean newFollowIdStr;
				int allZero;
				for (newDesignIdStr = 0; newDesignIdStr < form.getItems()
						.size(); ++newDesignIdStr) {
					newFollowIdStr = (BusinessBillItemBean) form.getItems()
							.get(newDesignIdStr);

					for (allZero = 0; allZero < statList.size(); ++allZero) {
						Object[] allFinish = (Object[]) statList.get(allZero);
						BusinessBillItemExt hasSetuItem = (BusinessBillItemExt) allFinish[0];
						long setuped = Long.parseLong(allFinish[1].toString());
						if (hasSetuItem.getId() == newFollowIdStr.getOldId()) {
							if (setuped != newFollowIdStr.getBusinessBillItem()
									.getWcId()) {
								hasSetuItem.setMaked("N");
								hasSetuItem.setMakerId(0L);
								hasSetuItem.setMakerName((String) null);
								hasSetuItem.setMakeTime((Date) null);
								hasSetuItem.setUrgentFlag("N");
								hasSetuItem.setProId(0L);
								hasSetuItem.setProFlag("N");
								newFollowIdStr.setWcChange(true);
								hasSetuItem.setCompStr("");
							}

							newFollowIdStr.setBusinessBillItemExt(hasSetuItem);
							break;
						}
					}

					if (newFollowIdStr.getBusinessBillItemExt() == null) {
						newFollowIdStr
								.setBusinessBillItemExt(new BusinessBillItemExt());
					}
				}

				BusinessBillItemExt arg47;
				for (newDesignIdStr = 0; newDesignIdStr < form.getItems()
						.size(); ++newDesignIdStr) {
					newFollowIdStr = (BusinessBillItemBean) form.getItems()
							.get(newDesignIdStr);
					arg47 = newFollowIdStr.getBusinessBillItemExt();
					if (arg47.getMaked().equals("Y")) {
						for (int arg48 = 0; arg48 < form.getItems().size(); ++arg48) {
							BusinessBillItemBean arg51 = (BusinessBillItemBean) form
									.getItems().get(arg48);
							if (newFollowIdStr.getBusinessBillItem().getWcId() == arg51
									.getBusinessBillItem().getWcId()) {
								arg51.getBusinessBillItemExt().setMaked(
										arg47.getMaked());
								arg51.getBusinessBillItemExt().setMakerId(
										arg47.getMakerId());
								arg51.getBusinessBillItemExt().setMakerName(
										arg47.getMakerName());
								arg51.getBusinessBillItemExt().setMakeTime(
										arg47.getMakeTime());
								arg51.getBusinessBillItemExt().setUrgentFlag(
										arg47.getUrgentFlag());
							}
						}
					}
				}

				for (newDesignIdStr = 0; newDesignIdStr < form.getItems()
						.size(); ++newDesignIdStr) {
					newFollowIdStr = (BusinessBillItemBean) form.getItems()
							.get(newDesignIdStr);
					arg47 = newFollowIdStr.getBusinessBillItemExt();
					if (arg47.getMaked().equals("Y")
							&& newFollowIdStr.getBusinessBillItem().getWcId() != 0L
							&& arg47.getProId() == 0L) {
						arg47.setProId(this.getFirstProId(proList,
								newFollowIdStr.getBusinessBillItem().getWcId()));
						arg47.setProFlag("N");
					}
				}

				this.setCost(form, ent.getBusiLengType());
				obj.setModifierId(ub.getStaffer().getId());
				obj.setModifierName(ub.getStaffer().getStafferName());
				obj.setModifyTime(new Date());
				obj.setUpdateTime(new Date());
				businessBillDao.update(obj);
				String arg45;
				if (be.getExamined().equals("Y")) {
					be.setCollectedCost(be.getReceiveCost().add(
							obj.getImprest()));
					if (obj.getCost().subtract(be.getCollectedCost())
							.add(be.getAdjustCost())
							.compareTo(new BigDecimal(0)) == 0) {
						be.setCollected("Y");
					} else {
						be.setCollected("N");
					}

					BigDecimal arg44 = new BigDecimal(0);
					arg45 = "Y";

					for (allZero = 0; allZero < form.getItems().size(); ++allZero) {
						BusinessBillItemBean arg50 = (BusinessBillItemBean) form
								.getItems().get(allZero);
						if (!arg50.getStatus().equals("DELETE")) {
							if (arg50.getBusinessBillItemExt().getDeliAmou()
									.compareTo(arg44) == 0) {
								arg45 = "N";
							} else if (arg50
									.getBusinessBillItem()
									.getAmount()
									.compareTo(
											arg50.getBusinessBillItemExt()
													.getDeliAmou()) == 1) {
								arg45 = "P";
								break;
							}
						}
					}

					be.setDelivered(arg45);
					boolean arg49 = true;
					boolean arg52 = true;
					boolean arg53 = false;

					for (int arg54 = 0; arg54 < form.getItems().size(); ++arg54) {
						BusinessBillItemBean completeFlag = (BusinessBillItemBean) form
								.getItems().get(arg54);
						if (!completeFlag.getStatus().equals("DELETE")
								&& completeFlag.getBusinessBillItem()
										.getIsSetu().equals("Y")) {
							arg53 = true;
							if (completeFlag.getBusinessBillItemExt()
									.getSetuAmou().compareTo(arg44) != 0) {
								arg49 = false;
							}

							if (completeFlag
									.getBusinessBillItem()
									.getAmount()
									.compareTo(
											completeFlag
													.getBusinessBillItemExt()
													.getSetuAmou()) == 1) {
								arg52 = false;
							}
						}
					}

					String arg55 = "N";
					if (arg53) {
						if (arg49) {
							arg55 = "N";
						} else if (arg52) {
							arg55 = "Y";
						} else {
							arg55 = "P";
						}
					}

					be.setSetuped(arg55);
					be.setExamSms("N");
					be.setMakeSms("Y");
					String arg56 = "Y";

					for (int i = 0; i < form.getItems().size(); ++i) {
						BusinessBillItemBean bean = (BusinessBillItemBean) form
								.getItems().get(i);
						if (!bean.getStatus().equals("DELETE")
								&& (bean.getBusinessBillItem().getWcId() != 0L
										&& bean.getBusinessBillItemExt()
												.getWcComp().equals("N") || bean
										.getBusinessBillItem().getIsCoop()
										.equals("Y")
										&& bean.getBusinessBillItemExt()
												.getCoopComp().equals("N"))) {
							arg56 = "N";
							break;
						}
					}

					be.setCompleted(arg56);
					if (be.getCompleted().equals("N")) {
						be.setCompleteTime((Date) null);
					} else {
						be.setCompleteTime(new Date());
					}

					if (obj.getImprest().compareTo(new BigDecimal(0)) != 0
							&& (be.getReceTime() == null || be.getReceTime()
									.before(obj.getOrderDate()))) {
						be.setReceTime(obj.getOrderDate());
					}
				} else {
					be.setAdjustCost(new BigDecimal(0));
					be.setCollectedCost(new BigDecimal(0));
					be.setCollected("N");
					be.setCompleted("N");
					be.setCompleteTime((Date) null);
					be.setExamSms("Y");
					be.setMakeSms("N");
				}

				businessBillDao.update(be);
				this.saveItems(obj.getId(), be.getHasDeliver(),
						be.getHasSetu(), form.getItems(), businessBillItemDao,
						outputDao, idGenerator);
				if (be.getExamined().equals("Y")) {
					this.updateProfit(obj.getId(), obj.getBillCode(),
							obj.getCost(), be.getAdjustCost(), businessBillDao,
							cooperBillItemDao, feeBillDao, stockDao,
							idGenerator);
				}

				if (be.getExamined().equals("Y")) {
					financeDetailDao.delete(obj.getId(), "BUSINESS_BILL");
				}

				if (be.getExamined().equals("Y")) {
					this.insertFinanceDetail(obj, businessBillDao, idGenerator,
							"UPDATE");
				}

				String arg46 = obj.getDesignerIdStr();
				if (arg46 == null) {
					arg46 = "";
				}

				if (oldDesignIdStr == null) {
					oldDesignIdStr = "";
				}

				if (!arg46.equals(oldDesignIdStr)) {
					businessBillDao.clearDesignDetail(obj.getId());
					this.insertDesignDetail(obj, businessBillDao, idGenerator);
				}

				arg45 = obj.getFollowerIdStr();
				if (arg45 == null) {
					arg45 = "";
				}

				if (oldFollowIdStr == null) {
					oldFollowIdStr = "";
				}

				if (!arg45.equals(oldFollowIdStr)) {
					businessBillDao.clearFollowDetail(obj.getId());
					this.insertFollowDetail(obj, businessBillDao, idGenerator);
				}

				if (editType.equals("PRICE") || editType.equals("MODIFY")) {
					this.saveUpdateLog(businessBillDao, form, oldDate,
							oldClientId, oldBaId, oldImprest, oldFavCost,
							oldCost, oldBillCode, oldDelDate, ub, idGenerator);
				}

			}
		}
	}

	private void saveUpdateLog(BaseDao baseDao, BusinessBillBean bean,
			Date orderDate, long clientId, long baId, BigDecimal imprest,
			BigDecimal favCost, BigDecimal cost, String billCode,
			Date deliverDate, UserBean ub, IdGenerator idGenerator) {
		BusinessUpdateLog log = null;
		BusinessBill b = bean.getBusinessBill();
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		boolean deliverDateChange = false;
		if (deliverDate == null
				&& b.getDeliverDate() != null
				|| deliverDate != null
				&& b.getDeliverDate() == null
				|| deliverDate != null
				&& b.getDeliverDate() != null
				&& !sf.format(deliverDate)
						.equals(sf.format(b.getDeliverDate()))) {
			deliverDateChange = true;
		}

		if (imprest.compareTo(b.getImprest()) != 0
				|| !sf.format(orderDate).equals(sf.format(b.getOrderDate()))
				|| cost.compareTo(b.getCost()) != 0
				|| favCost.compareTo(b.getFavCost()) != 0
				|| clientId != b.getClientId() || baId != b.getBankAccountId()
				|| !billCode.equals(b.getBillCode()) || deliverDateChange) {
			log = new BusinessUpdateLog();
			log.setId(idGenerator.getId(BusinessUpdateLog.class.getName()));
			log.setBillId(b.getId());
			log.setUpdateId(ub.getStaffer().getId());
			log.setUpdateName(ub.getStaffer().getStafferName());
			if (!sf.format(orderDate).equals(sf.format(b.getOrderDate()))) {
				log.setOrderDate(sf.format(orderDate) + " --> "
						+ sf.format(b.getOrderDate()));
			}

			if (clientId != b.getClientId()) {
				Establishment oldStr = (Establishment) baseDao.get(
						Establishment.class, clientId);
				log.setClientName(oldStr.getEstName() + " --> "
						+ bean.getClientName());
			}

			if (baId != b.getBankAccountId()) {
				BankAccount oldStr1 = (BankAccount) baseDao.get(
						BankAccount.class, baId);
				BankAccount newStr = (BankAccount) baseDao.get(
						BankAccount.class, b.getBankAccountId());
				String baOldName = "";
				String baNewName = "";
				if (oldStr1 != null) {
					baOldName = oldStr1.getBankName();
					if (oldStr1.getAccountName() != null
							&& !oldStr1.getAccountName().equals("")) {
						baOldName = baOldName + "(" + oldStr1.getAccountName()
								+ ")";
					}
				}

				if (newStr != null) {
					baNewName = newStr.getBankName();
					if (newStr.getAccountName() != null
							&& !newStr.getAccountName().equals("")) {
						baNewName = baNewName + "(" + newStr.getAccountName()
								+ ")";
					}
				}

				log.setBackAccount(baOldName + " --> " + baNewName);
			}

			if (imprest.compareTo(b.getImprest()) != 0) {
				log.setImprest(EncCommonHelper.parseBigDecimal(imprest)
						+ " --> " + b.getImprest());
			}

			if (favCost.compareTo(b.getFavCost()) != 0) {
				log.setFavCost(EncCommonHelper.parseBigDecimal(favCost)
						+ " --> " + b.getFavCost());
			}

			if (cost.compareTo(b.getCost()) != 0) {
				log.setCost(EncCommonHelper.parseBigDecimal(cost) + " --> "
						+ b.getCost());
			}

			if (!billCode.equals(b.getBillCode())) {
				log.setCode(billCode + " --> " + b.getBillCode());
			}

			if (deliverDateChange) {
				String oldStr2 = "NULL";
				String newStr1 = "";
				if (deliverDate != null) {
					oldStr2 = sf.format(deliverDate);
				}

				if (b.getDeliverDate() != null) {
					newStr1 = sf.format(b.getDeliverDate());
				}

				log.setDeliverDate(oldStr2 + " --> " + newStr1);
			}

			baseDao.save(log);
		}

	}

	private void setCost(BusinessBillBean form, String busiLengType) {
		List items = form.getItems();
		BigDecimal cost = new BigDecimal(0);
		BigDecimal amount = new BigDecimal(0);
		BigDecimal square = new BigDecimal(0);
		BigDecimal lengM = new BigDecimal(0);
		BigDecimal lengCm = new BigDecimal(0);

		for (int i = 0; i < items.size(); ++i) {
			BusinessBillItemBean bean = (BusinessBillItemBean) items.get(i);
			BusinessBillItem item = bean.getBusinessBillItem();
			if (!bean.getStatus().equals("DELETE")) {
				BigDecimal squa;
				if (item.getPriceType().equals("AREA")) {
					squa = item.getLength().multiply(item.getWidth())
							.multiply(item.getAmount());
					if (item.getLengType().equals("MM")) {
						squa = squa.divide(new BigDecimal(1000000), 3, 4);
					} else {
						squa = squa.setScale(3, 4);
					}

					item.setSquareNum(squa);
					item.setCost(item.getUnitPrice().multiply(squa)
							.setScale(2, 4));
					square = square.add(squa);
				} else if (item.getPriceType().equals("AMOUNT")) {
					item.setCost(item.getUnitPrice().multiply(item.getAmount())
							.setScale(2, 4));
					amount = amount.add(item.getAmount());
				} else if (item.getPriceType().equals("LENGTH")) {
					if (busiLengType.equals("WIDTH")) {
						squa = item.getAmount().multiply(item.getLength());
					} else if (busiLengType.equals("TALL")) {
						squa = item.getAmount().multiply(item.getWidth());
					} else if (item.getLength().compareTo(item.getWidth()) > 0) {
						squa = item.getAmount().multiply(item.getLength());
					} else {
						squa = item.getAmount().multiply(item.getWidth());
					}

					squa = squa.setScale(2, 4);
					item.setSquareNum(squa);
					item.setCost(item.getUnitPrice().multiply(squa)
							.setScale(2, 4));
					if (item.getLengType().equals("M")) {
						lengM = lengM.add(squa);
					} else {
						lengCm = lengCm.add(squa);
					}
				}

				if (bean.getStatus().equals("UNCHANGE")) {
					bean.setStatus("EDIT");
				}

				cost = cost.add(item.getCost());
			}
		}

		cost = cost.subtract(form.getBusinessBill().getFavCost());
		form.getBusinessBill().setCost(cost);
		form.getBusinessBill().setAmount(amount);
		form.getBusinessBill().setSquare(square);
		form.getBusinessBill().setLengCm(lengCm);
		form.getBusinessBill().setLengM(lengM);
	}

	private void saveItems(long billId, String hasDeli, String hasSetu,
			List items, BusinessBillItemDao businessBillItemDao,
			OutputDao outputDao, IdGenerator idGenerator) throws Exception {
		int newNum = 0;

		for (int curItemId = 0; curItemId < items.size(); ++curItemId) {
			BusinessBillItemBean bean = (BusinessBillItemBean) items
					.get(curItemId);
			if (bean.getStatus().equals("NEW")) {
				++newNum;
			}
		}

		long arg13 = 0L;
		if (newNum > 0) {
			arg13 = idGenerator.getId(BusinessBillItem.class.getName(), newNum);
		}

		for (int i = 0; i < items.size(); ++i) {
			BusinessBillItemBean bean1 = (BusinessBillItemBean) items.get(i);
			bean1.getBusinessBillItem().setBillId(billId);
			if (bean1.getStatus().equals("DELETE")) {
				if (bean1.getOldId() != 0L) {
					if (hasDeli.equals("Y")) {
						throw new NotAllowException("业务单已经送货，不能删除明细项目！");
					}

					if (hasSetu.equals("Y")) {
						throw new NotAllowException("业务单已经安装，不能删除明细项目！");
					}

					businessBillItemDao.delete(bean1.getOldId());
					businessBillItemDao.deleteExt(bean1.getOldId());
					outputDao.deleteForBbItem(bean1.getOldId());
				}
			} else if (bean1.getStatus().equals("EDIT")) {
				if (bean1.isWcChange()) {
					outputDao.deleteForBbItem(bean1.getOldId());
				}

				businessBillItemDao.update(bean1.getBusinessBillItem());
				businessBillItemDao.update(bean1.getBusinessBillItemExt());
			} else if (bean1.getStatus().equals("NEW")) {
				bean1.getBusinessBillItem().setId(arg13);
				businessBillItemDao.save(bean1.getBusinessBillItem());
				++arg13;
				bean1.getBusinessBillItemExt().setId(
						bean1.getBusinessBillItem().getId());
				businessBillItemDao.save(bean1.getBusinessBillItemExt());
			}
		}

	}

	private long getFirstProId(List proList, long wcId) {
		WorkProcess p = this.getFirstPro(proList, wcId);
		return p != null ? p.getId() : 0L;
	}

	private WorkProcess getFirstPro(List proList, long wcId) {
		WorkProcess p = null;

		for (int i = 0; i < proList.size(); ++i) {
			p = (WorkProcess) proList.get(i);
			if (p.getEnabled().equals("Y") && p.getWorkCenterId() == wcId) {
				return p;
			}
		}

		return null;
	}

	public Map txExamine(long id, boolean noExamOthe,
			BusinessBillDao businessBillDao,
			BusinessBillItemDao businessBillItemDao,
			ReceiveBillDao receiveBillDao, AdjustBillDao adjustBillDao,
			long examinerId, String examinerName,
			BankAccountBalanceDao bankAccountBalanceDao,
			CooperBillItemDao cooperBillItemDao, FeeBillDao feeBillDao,
			StockDao stockDao, IdGenerator idGenerator) throws Exception {
		BusinessBill b = (BusinessBill) businessBillDao.get(BusinessBill.class,
				id);
		BusinessBillExt be = (BusinessBillExt) businessBillDao.get(
				BusinessBillExt.class, id);
		HashMap map = new HashMap();
		if (b != null && be.getEnabled().equals("Y")
				&& be.getExamined().equals("N") && be.getClosed().equals("N")) {
			if (noExamOthe
					&& (b.getFollowerIdStr() == null || b.getFollowerIdStr()
							.indexOf(";" + examinerId + ":") == -1)) {
				throw new NotAllowException("对不起，您没有权限审核其他人跟单的单据！");
			}

			List bbItemList = businessBillItemDao.getListWidthExt(id);
			this.doExamine(b, be, businessBillDao, receiveBillDao,
					adjustBillDao, examinerId, examinerName,
					bankAccountBalanceDao, cooperBillItemDao, feeBillDao,
					stockDao, (OutputDao) null, idGenerator, bbItemList,
					(List) null, "MULTI_EXA");
			map.put("exam", "Y");
			map.put("content", b.getContent());
			map.put("billCode", b.getBillCode());
			map.put("phone", b.getPhone());
			map.put("linkman", b.getLinkman());
		}

		return map;
	}

	public Map txSingleExamine(long id, boolean noExamOthe, String wcIdStr,
			String cooperStr, String accWcIdStr, String accTypeStr,
			BusinessBillDao businessBillDao,
			BusinessBillItemDao businessBillItemDao, AccessoryDao accessoryDao,
			ReceiveBillDao receiveBillDao, AdjustBillDao adjustBillDao,
			long examinerId, String examinerName,
			BankAccountBalanceDao bankAccountBalanceDao,
			CooperBillItemDao cooperBillItemDao, FeeBillDao feeBillDao,
			StockDao stockDao, OutputDao outputDao, IdGenerator idGenerator)
			throws Exception {
		BusinessBill b = (BusinessBill) businessBillDao.get(BusinessBill.class,
				id);
		BusinessBillExt be = (BusinessBillExt) businessBillDao.get(
				BusinessBillExt.class, id);
		HashMap map = new HashMap();
		if (b != null && be.getEnabled().equals("Y")
				&& be.getExamined().equals("N") && be.getClosed().equals("N")) {
			if (noExamOthe
					&& (b.getFollowerIdStr() == null || b.getFollowerIdStr()
							.indexOf(";" + examinerId + ":") == -1)) {
				throw new NotAllowException("对不起，您没有权限审核其他人跟单的单据！");
			}

			HashMap wcMap = new HashMap();
			String[] tmpArr = wcIdStr.split(";");

			String[] vArr;
			String vId;
			String vValue;
			for (int accWcMap = 0; accWcMap < tmpArr.length; ++accWcMap) {
				if (tmpArr[accWcMap] != null && !tmpArr.equals("")) {
					vArr = tmpArr[accWcMap].split(":");
					vId = "";
					vValue = "";
					if (vArr.length > 0) {
						vId = vArr[0];
					}

					if (vArr.length > 1) {
						vValue = vArr[1];
					}

					wcMap.put(vId, vValue);
				}
			}

			HashMap arg37 = new HashMap();
			tmpArr = accWcIdStr.split(";");

			for (int accTypeMap = 0; accTypeMap < tmpArr.length; ++accTypeMap) {
				if (tmpArr[accTypeMap] != null && !tmpArr.equals("")) {
					vArr = tmpArr[accTypeMap].split(":");
					vId = "";
					vValue = "";
					if (vArr.length > 0) {
						vId = vArr[0];
					}

					if (vArr.length > 1) {
						vValue = vArr[1];
					}

					arg37.put(vId, vValue);
				}
			}

			HashMap arg38 = new HashMap();
			tmpArr = accTypeStr.split(";");

			for (int bbItemList = 0; bbItemList < tmpArr.length; ++bbItemList) {
				if (tmpArr[bbItemList] != null && !tmpArr.equals("")) {
					vArr = tmpArr[bbItemList].split(":");
					vId = "";
					vValue = "";
					if (vArr.length > 0) {
						vId = vArr[0];
					}

					if (vArr.length > 1) {
						vValue = vArr[1];
					}

					arg38.put(vId, vValue);
				}
			}

			List arg39 = businessBillItemDao.getListWidthExt(id);
			ArrayList wcChangedArr = new ArrayList();

			for (int accItemList = 0; accItemList < arg39.size(); ++accItemList) {
				Object[] i = (Object[]) arg39.get(accItemList);
				BusinessBillItem item = (BusinessBillItem) i[0];
				BusinessBillItemExt ie = (BusinessBillItemExt) i[1];
				if (cooperStr.indexOf(";" + item.getId() + ";") != -1) {
					item.setIsCoop("Y");
				} else {
					item.setIsCoop("N");
				}

				if (wcMap.containsKey(String.valueOf(item.getId()))) {
					if (item.getWcId() != Long.parseLong((String) wcMap
							.get(String.valueOf(item.getId())))) {
						ie.setCompStr("");
						businessBillDao.update(ie);
						wcChangedArr.add(String.valueOf(item.getId()));
					}

					item.setWcId(Long.parseLong((String) wcMap.get(String
							.valueOf(item.getId()))));
				}

				businessBillDao.update(item);
			}

			List arg40 = accessoryDao.getList("BUSINESS_BILL", id,
					(String[]) null, 0L);

			for (int arg41 = 0; arg41 < arg40.size(); ++arg41) {
				Accessory arg42 = (Accessory) arg40.get(arg41);
				if (arg37.containsKey(String.valueOf(arg42.getId()))) {
					arg42.setExtId(Long.parseLong((String) arg37.get(String
							.valueOf(arg42.getId()))));
				}

				if (arg38.containsKey(String.valueOf(arg42.getId()))) {
					arg42.setAccType((String) arg38.get(String.valueOf(arg42
							.getId())));
				}

				if (arg42.getAccType() == null
						|| !arg42.getAccType().equals("DESIGN")) {
					arg42.setExtId(0L);
				}

				accessoryDao.update(arg42);
			}

			b.setUpdateTime(new Date());
			businessBillDao.update(b);
			this.doExamine(b, be, businessBillDao, receiveBillDao,
					adjustBillDao, examinerId, examinerName,
					bankAccountBalanceDao, cooperBillItemDao, feeBillDao,
					stockDao, outputDao, idGenerator, arg39, wcChangedArr,
					"SINGLE_EXA");
			map.put("exam", "Y");
			map.put("content", b.getContent());
			map.put("billCode", b.getBillCode());
			map.put("phone", b.getPhone());
			map.put("linkman", b.getLinkman());
		}

		return map;
	}

	private void doExamine(BusinessBill b, BusinessBillExt be,
			BusinessBillDao businessBillDao, ReceiveBillDao receiveBillDao,
			AdjustBillDao adjustBillDao, long examinerId, String examinerName,
			BankAccountBalanceDao bankAccountBalanceDao,
			CooperBillItemDao cooperBillItemDao, FeeBillDao feeBillDao,
			StockDao stockDao, OutputDao outputDao, IdGenerator idGenerator,
			List itemList, List wcChangedArr, String fdMark) throws Exception {
		String clientName;
		if (itemList != null) {
			clientName = "Y";

			for (int est = 0; est < itemList.size(); ++est) {
				Object[] i = (Object[]) itemList.get(est);
				BusinessBillItem item = (BusinessBillItem) i[0];
				BusinessBillItemExt ie = (BusinessBillItemExt) i[1];
				if (item.getWcId() != 0L && ie.getWcComp().equals("N")
						|| item.getIsCoop().equals("Y")
						&& ie.getCoopComp().equals("N")) {
					clientName = "N";
					break;
				}
			}

			be.setCompleted(clientName);
			if (be.getCompleted().equals("N")) {
				be.setCompleteTime((Date) null);
			} else {
				be.setCompleteTime(new Date());
			}
		}

		be.setCollectedCost(b.getImprest());
		if (b.getCost().subtract(be.getCollectedCost())
				.compareTo(new BigDecimal(0)) == 0) {
			be.setCollected("Y");
		}

		if (b.getImprest().compareTo(new BigDecimal(0)) != 0) {
			be.setReceTime(b.getOrderDate());
		}

		be.setExamined("Y");
		be.setExamSms("N");
		be.setMakeSms("Y");
		be.setExamineTime(new Date());
		be.setExaminerId(examinerId);
		be.setExaminerName(examinerName);
		businessBillDao.update(be);
		clientName = "";
		Establishment arg22 = (Establishment) bankAccountBalanceDao.get(
				Establishment.class, b.getClientId());
		if (arg22 != null) {
			clientName = arg22.getEstName();
			if (b.getContent() != null && !b.getContent().equals("")) {
				clientName = clientName + "," + b.getContent();
			}
		}

		PageHelper.updateBankAccount(b.getBankAccountId(), b.getTallySetId(),
				b.getId(), b.getImprest(), b.getOrderDate(),
				be.getExamineTime(), b.getBillCode(), "BUSINESS_BILL", "",
				"业务预收", "R", b.getManagerName(), clientName, "BUSINESS",
				"EXAM", false, (BankAccountDetailDao) null,
				bankAccountBalanceDao, idGenerator);
		this.updateEstablishment(b.getClientId(), b.getOrderDate(),
				b.getCost(), businessBillDao);
		this.updateProfit(b.getId(), b.getBillCode(), b.getCost(),
				be.getAdjustCost(), businessBillDao, cooperBillItemDao,
				feeBillDao, stockDao, idGenerator);
		this.insertFinanceDetail(b, businessBillDao, idGenerator, fdMark);
		if (wcChangedArr != null) {
			for (int arg23 = 0; arg23 < wcChangedArr.size(); ++arg23) {
				outputDao.deleteForBbItem(Long.parseLong(wcChangedArr
						.get(arg23).toString()));
			}
		}

	}

	public void txUnExamine(long id, BusinessBillItemDao businessBillItemDao,
			BankAccountBalanceDao bankAccountBalanceDao,
			BankAccountDetailDao bankAccountDetailDao,
			FinanceDetailDao financeDetailDao, IdGenerator idGenerator,
			UserBean ub) throws Exception {
		BusinessBill b = (BusinessBill) businessBillItemDao.get(
				BusinessBill.class, id);
		BusinessBillExt be = (BusinessBillExt) businessBillItemDao.get(
				BusinessBillExt.class, id);
		if (b != null && be.getEnabled().equals("Y")
				&& be.getExamined().equals("Y")) {
			if (be.getClosed().equals("Y")) {
				throw new NotAllowException("业务单" + b.getBillCode()
						+ "已经结清，不能反审核!");
			}

			if (be.getHasDeliver().equals("Y")) {
				throw new NotAllowException("业务单" + b.getBillCode()
						+ "已经送货，不能反审核!");
			}

			if (be.getHasSetu().equals("Y")) {
				throw new NotAllowException("业务单" + b.getBillCode()
						+ "已经安装，不能反审核!");
			}

			if (be.getMaked().equals("Y")) {
				throw new NotAllowException("业务单" + b.getBillCode()
						+ "已经制作，不能反审核!");
			}

			if (be.getHasReceive().equals("Y")) {
				throw new NotAllowException("业务单" + b.getBillCode()
						+ "已经收款，不能反审核!");
			}

			if (be.getHasAdjust().equals("Y")) {
				throw new NotAllowException("业务单" + b.getBillCode()
						+ "已经调账，不能反审核!");
			}

			be.setExamined("N");
			be.setExamSms("Y");
			be.setMakeSms("N");
			be.setAdjustCost(new BigDecimal(0));
			be.setCollected("N");
			be.setReceiveCost(new BigDecimal(0));
			be.setCollectedCost(new BigDecimal(0));
			be.setExaminerId(0L);
			be.setExaminerName("");
			be.setReceTime((Date) null);
			businessBillItemDao.update(be);
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
			String mark = ub.getStaffer().getStafferName() + "于"
					+ sf.format(new Date()) + "反审核单据";
			PageHelper.updateBankAccount(b.getBankAccountId(),
					b.getTallySetId(), b.getId(),
					(new BigDecimal(0)).subtract(b.getImprest()),
					b.getOrderDate(), new Date(), b.getBillCode(),
					"BUSINESS_BILL", "", "业务预收", "R", b.getManagerName(), mark,
					"BUSINESS", "UN_EXAM", true, bankAccountDetailDao,
					bankAccountBalanceDao, idGenerator);
			Establishment est = (Establishment) bankAccountBalanceDao.get(
					Establishment.class, b.getClientId());
			if (est != null) {
				est.setBusCost(est.getBusCost().subtract(b.getCost()));
				businessBillItemDao.update(est);
			}

			List itemList = businessBillItemDao.getListWidthExt(b.getId());

			for (int i = 0; i < itemList.size(); ++i) {
				Object[] objs = (Object[]) itemList.get(i);
				BusinessBillItemExt ie = (BusinessBillItemExt) objs[1];
				if (ie.getMaked().equals("Y")) {
					ie.setMaked("N");
					ie.setMakerId(0L);
					ie.setMakerName("");
					ie.setMakeTime((Date) null);
					businessBillItemDao.update(ie);
				}
			}

			financeDetailDao.delete(b.getId(), "BUSINESS_BILL");
		}

	}

	public String txCancel(long id, String reason,
			BusinessBillDao businessBillDao, UserBean ub,
			BankAccountBalanceDao bankAccountBalanceDao,
			BankAccountDetailDao bankAccountDetailDao,
			FinanceDetailDao financeDetailDao, AccessoryDao accessoryDao,
			String filePath, IdGenerator idGenerator) throws Exception {
		String msg = "";
		BusinessBill b = (BusinessBill) businessBillDao.get(BusinessBill.class,
				id);
		BusinessBillExt be = (BusinessBillExt) businessBillDao.get(
				BusinessBillExt.class, id);
		if (b != null && be.getEnabled().equals("Y")) {
			if (be.getClosed().equals("Y")) {
				msg = "单号为 " + b.getBillCode() + " 的业务单已经结清，不能作废!";
			} else if (be.getHasReceive().equals("Y")) {
				msg = "单号为 " + b.getBillCode()
						+ " 的业务单已经收款，不能作废!请点击“关联单据”，先作废业务单的关联单据，然后再作废业务单。";
			} else if (be.getHasAdjust().equals("Y")) {
				msg = "单号为 " + b.getBillCode()
						+ " 的业务单已经调账，不能作废!请点击“关联单据”，先作废业务单的关联单据，然后再作废业务单。";
			} else if (be.getHasDeliver().equals("Y")) {
				msg = "单号为 " + b.getBillCode()
						+ " 的业务单已经送货，不能作废!请点击“关联单据”，先作废业务单的关联单据，然后再作废业务单。";
			} else if (be.getHasSetu().equals("Y")) {
				msg = "单号为 " + b.getBillCode()
						+ " 的业务单已经安装，不能作废!请点击“关联单据”，先作废业务单的关联单据，然后再作废业务单。";
			} else {
				be.setEnabled("N");
				be.setExamSms("N");
				be.setMakeSms("N");
				be.setDisableManId(ub.getUser().getStafferId());
				businessBillDao.update(be);
				if (be.getExamined().equals("Y")) {
					SimpleDateFormat accList = new SimpleDateFormat(
							"yyyy-MM-dd HH:mm");
					String log = ub.getStaffer().getStafferName() + "于"
							+ accList.format(new Date()) + "作废单据";
					PageHelper.updateBankAccount(b.getBankAccountId(),
							b.getTallySetId(), b.getId(),
							(new BigDecimal(0)).subtract(b.getImprest()),
							b.getOrderDate(), new Date(), b.getBillCode(),
							"BUSINESS_BILL", "", "业务预收", "R",
							b.getManagerName(), log, "BUSINESS", "CANC", true,
							bankAccountDetailDao, bankAccountBalanceDao,
							idGenerator);
					Establishment a = (Establishment) businessBillDao.get(
							Establishment.class, b.getClientId());
					if (a != null) {
						a.setBusCost(a.getBusCost().subtract(b.getCost()));
						businessBillDao.update(a);
					}
				}

				if (be.getExamined().equals("Y")) {
					financeDetailDao.delete(b.getId(), "BUSINESS_BILL");
				}

				List arg18 = accessoryDao.getList("BUSINESS_BILL", b.getId(),
						(String[]) null, 0L);

				for (int arg19 = 0; arg19 < arg18.size(); ++arg19) {
					Accessory arg21 = (Accessory) arg18.get(arg19);
					if (arg21 != null && arg21.getFileName() != null
							&& !arg21.getFileName().equals("")) {
						File file = new File(filePath + arg21.getFileName());
						if (file.exists() && file.isFile()) {
							file.delete();
						}
					}
				}

				accessoryDao.delete("BUSINESS_BILL", b.getId());
				SystemLog arg20 = new SystemLog();
				arg20.setId(idGenerator.getId(SystemLog.class.getName()));
				arg20.setActionCode("BUSINESS_BILL");
				arg20.setLogType("CANCEL_LOG");
				arg20.setCreaterId(ub.getStaffer().getId());
				arg20.setCreaterName(ub.getStaffer().getStafferName());
				arg20.setDepName(ub.getDepName());
				arg20.setDepId(ub.getStaffer().getDepartmentId());
				arg20.setStatus("S");
				arg20.setUserName(ub.getUser().getUserName());
				arg20.setIp(b.getBillCode());
				arg20.setMark("");
				arg20.setRelId(id);
				arg20.setCancelReason(reason);
				arg20.setTallySetId(b.getTallySetId());
				businessBillDao.save(arg20);
				msg = "作废成功";
			}
		}

		return msg;
	}

	public Map txWcComplete(long billId, long wcId, long firstProId,
			String itemIdStr, CodeRule obCr, List wcList, List mtList,
			UserBean ub, boolean noCompOthe, BusinessBillDao businessBillDao,
			BusinessBillItemDao businessBillItemDao, OutBillDao outBillDao,
			StockDao stockDao, StockBalanceDao stockBalanceDao,
			SerialNumberDao serialNumberDao, IdGenerator idGenerator)
			throws Exception {
		if (obCr == null) {
			throw new Exception("Code rule not found for out bill");
		} else {
			HashMap map = new HashMap();
			BusinessBill b = (BusinessBill) businessBillItemDao.get(
					BusinessBill.class, billId);
			BusinessBillExt be = (BusinessBillExt) businessBillItemDao.get(
					BusinessBillExt.class, billId);
			if (b != null) {
				if (noCompOthe
						&& (b.getFollowerIdStr() == null || b
								.getFollowerIdStr().indexOf(
										";" + ub.getStaffer().getId() + ":") == -1)) {
					throw new NotAllowException("对不起，您没有权限完工其他人跟单的单据！");
				}

				String[] itemIdArr = (String[]) null;
				if (itemIdStr != null) {
					itemIdArr = itemIdStr.split(";");
				}

				Date now = new Date();
				List itemList = businessBillItemDao.getListWidthExt(billId);
				boolean itemChange = false;

				for (int mComp = 0; mComp < itemList.size(); ++mComp) {
					Object[] cComp = (Object[]) itemList.get(mComp);
					BusinessBillItem completed = (BusinessBillItem) cComp[0];
					BusinessBillItemExt msgContent = (BusinessBillItemExt) cComp[1];
					boolean i = false;
					if (itemIdArr != null) {
						for (int objs = 0; objs < itemIdArr.length; ++objs) {
							if (completed.getId() == Long
									.parseLong(itemIdArr[objs])
									&& completed.getWcId() == wcId) {
								i = true;
								break;
							}
						}
					} else if (wcId == 0L) {
						i = true;
					} else if (completed.getWcId() == wcId) {
						i = true;
					}

					if (i && msgContent.getWcComp().equals("N")) {
						msgContent.setWcComp("Y");
						msgContent.setOperId(ub.getStaffer().getId());
						msgContent
								.setOperName(ub.getStaffer().getStafferName());
						msgContent.setOperTime(now);
						if (msgContent.getMaked().equals("N")) {
							msgContent.setMaked("Y");
							msgContent.setMakerId(ub.getStaffer().getId());
							msgContent.setMakerName(ub.getStaffer()
									.getStafferName());
							msgContent.setMakeTime(now);
							msgContent.setProFlag("N");
							msgContent.setProId(firstProId);
						}

						businessBillItemDao.update(msgContent);
						itemChange = true;
					}
				}

				if (itemChange) {
					String arg33 = "Y";
					String arg34 = "Y";

					for (int arg35 = 0; arg35 < itemList.size(); ++arg35) {
						Object[] arg37 = (Object[]) itemList.get(arg35);
						BusinessBillItem arg39 = (BusinessBillItem) arg37[0];
						BusinessBillItemExt arg40 = (BusinessBillItemExt) arg37[1];
						if (arg39.getWcId() != 0L
								&& arg40.getWcComp().equals("N")) {
							arg33 = "N";
						}

						if (arg39.getIsCoop().equals("Y")
								&& arg40.getCoopComp().equals("N")) {
							arg34 = "N";
						}
					}

					String arg36 = "N";
					if (arg33.equals("Y") && arg34.equals("Y")) {
						arg36 = "Y";
					}

					be.setCompleted(arg36);
					if (be.getCompleted().equals("N")) {
						be.setCompleteTime((Date) null);
					} else {
						be.setCompleteTime(new Date());
					}

					businessBillItemDao.update(be);
					if (arg33.equals("Y")) {
						this.createOutBill(itemList, mtList, wcList, b, be, ub,
								obCr, businessBillDao, outBillDao, stockDao,
								stockBalanceDao, serialNumberDao, idGenerator);
					}

					if (arg36.equals("Y")) {
						String arg38 = "";

						for (int arg41 = 0; arg41 < itemList.size(); ++arg41) {
							Object[] arg42 = (Object[]) itemList.get(arg41);
							BusinessBillItem item = (BusinessBillItem) arg42[0];
							if (!arg38.equals("")) {
								arg38 = arg38 + "<br>";
							}

							arg38 = arg38
									+ PageHelper.createMsgContent(item, mtList);
						}

						map.put("comp", "Y");
						map.put("billId", Long.valueOf(b.getId()));
						map.put("billCode", b.getBillCode());
						map.put("estId", Long.valueOf(b.getClientId()));
						map.put("msgContent", arg38);
						map.put("follIdStr", b.getFollowerIdStr());
						map.put("follNameStr", b.getFollowerNameStr());
						map.put("ordeDate", b.getOrderDate());
						map.put("deliDate", b.getDeliverDate());
						map.put("phone", b.getPhone());
						map.put("linkman", b.getLinkman());
						map.put("content", b.getContent());
					}
				}
			}

			return map;
		}
	}

	private void createOutBill(List itemList, List mtList, List wcList,
			BusinessBill b, BusinessBillExt be, UserBean ub, CodeRule obCr,
			BusinessBillDao businessBillDao, OutBillDao outBillDao,
			StockDao stockDao, StockBalanceDao stockBalanceDao,
			SerialNumberDao serialNumberDao, IdGenerator idGenerator)
			throws Exception {
		HashMap wcMap = new HashMap();
		BigDecimal hund = new BigDecimal(100);

		for (int it = 0; it < itemList.size(); ++it) {
			Object[] key = (Object[]) itemList.get(it);
			BusinessBillItem stoId = (BusinessBillItem) key[0];
			if (stoId.getWcId() != 0L && stoId.getMakeTypeId() != 0L
					&& stoId.getProId() != 0L) {
				boolean wcName = false;
				BigDecimal outObjs1 = null;

				for (int ob = 0; ob < mtList.size(); ++ob) {
					MakeType obiList = (MakeType) mtList.get(ob);
					if (obiList.getId() == stoId.getMakeTypeId()
							&& obiList.getAutoStoc().equals("Y")) {
						outObjs1 = obiList.getLossRate();
						wcName = true;
						break;
					}
				}

				if (wcName) {
					String arg37 = String.valueOf(stoId.getWcId());
					if (wcMap.containsKey(arg37)) {
						List outObjs = (List) wcMap.get(arg37);
						outObjs.add(new Object[]{stoId, outObjs1});
					} else {
						ArrayList arg34 = new ArrayList();
						arg34.add(new Object[]{stoId, outObjs1});
						wcMap.put(arg37, arg34);
					}
				}
			}
		}

		if (!wcMap.isEmpty()) {
			Iterator arg31 = wcMap.keySet().iterator();

			while (true) {
				while (true) {
					String arg32;
					long arg33;
					String arg35;
					do {
						if (!arg31.hasNext()) {
							return;
						}

						arg32 = (String) arg31.next();
						arg33 = 0L;
						arg35 = "";

						for (int arg36 = 0; arg36 < wcList.size(); ++arg36) {
							WorkCenter arg38 = (WorkCenter) wcList.get(arg36);
							if (arg38.getId() == Long.parseLong(arg32)) {
								arg33 = arg38.getStorehouseId();
								arg35 = arg38.getWcName();
								break;
							}
						}
					} while (arg33 == 0L);

					List arg39 = (List) wcMap.get(arg32);
					OutBill arg40 = new OutBill();
					if (obCr.getGenerateRule().equals("MANUAL")) {
						throw new NotAllowException("出库单编码规则必须设置为自动产生！");
					}

					arg40.setBillCode(PageHelper.generateCode(serialNumberDao,
							obCr, "", b.getTallySetId()));

					for (OutBill arg41 = outBillDao.get(arg40.getBillCode()); arg41 != null; arg41 = outBillDao
							.get(arg40.getBillCode())) {
						arg40.setBillCode(PageHelper.generateCode(
								serialNumberDao, obCr, "", b.getTallySetId()));
					}

					arg40.setBusinessBillCode(b.getBillCode());
					arg40.setCreaterId(ub.getStaffer().getId());
					arg40.setCreaterName(ub.getStaffer().getStafferName());
					arg40.setCreateTime(new Date());
					arg40.setId(idGenerator.getId(OutBill.class.getName()));
					arg40.setManagerId(ub.getStaffer().getId());
					arg40.setManagerName(ub.getStaffer().getStafferName());
					arg40.setMark("业务单完工自动扣库存");
					arg40.setOutDate(new Date());
					arg40.setRequirerId(Long.parseLong(arg32));
					arg40.setRequirerType("WORK_CENTER");
					arg40.setStorehouseId(arg33);
					arg40.setTallySetId(b.getTallySetId());
					ArrayList arg42 = new ArrayList();

					for (int stockOk = 0; stockOk < arg39.size(); ++stockOk) {
						Object[] j = (Object[]) arg39.get(stockOk);
						BusinessBillItem obi = (BusinessBillItem) j[0];
						BigDecimal s = (BigDecimal) j[1];
						BigDecimal amount;
						if (obi.getPriceType().equals("AMOUNT")) {
							amount = obi.getAmount().add(
									obi.getAmount().multiply(s)
											.divide(hund, 2, 4));
						} else {
							amount = obi.getSquareNum().add(
									obi.getSquareNum().multiply(s)
											.divide(hund, 2, 4));
						}

						boolean obiFound = false;

						for (int obi1 = 0; obi1 < arg42.size(); ++obi1) {
							OutBillItem obi2 = (OutBillItem) arg42.get(obi1);
							if (obi2.getProductId() == obi.getProId()) {
								obi2.setAmount(obi2.getAmount().add(amount));
								obiFound = true;
								break;
							}
						}

						if (!obiFound) {
							OutBillItem arg49 = new OutBillItem();
							arg49.setAmount(amount);
							arg49.setBillId(arg40.getId());
							arg49.setId(idGenerator.getId(OutBillItem.class
									.getName()));
							arg49.setMark("业务单完工自动扣库存");
							arg49.setProductId(obi.getProId());
							arg42.add(arg49);
						}
					}

					boolean arg43 = true;

					int arg44;
					OutBillItem arg45;
					for (arg44 = 0; arg44 < arg42.size(); ++arg44) {
						arg45 = (OutBillItem) arg42.get(arg44);
						Stock arg46 = stockDao.get(arg45.getProductId(),
								arg40.getStorehouseId());
						if (arg46 == null
								|| arg46.getAmount().compareTo(
										arg45.getAmount()) < 0) {
							arg43 = false;
							break;
						}
					}

					if (arg43) {
						arg40.setExamined("Y");
						arg40.setExaminerId(ub.getStaffer().getId());
						arg40.setExaminerName(ub.getStaffer().getStafferName());
						arg40.setExamineTime(new Date());
						outBillDao.save(arg40);

						for (arg44 = 0; arg44 < arg42.size(); ++arg44) {
							arg45 = (OutBillItem) arg42.get(arg44);
							outBillDao.save(arg45);
						}

						BigDecimal arg47 = PageHelper.updateOutBillStock(arg40,
								arg42, stockDao, stockBalanceDao, idGenerator,
								arg35, "SAVE_EXAM", "", "");
						BusinessProfit arg48 = businessBillDao
								.getBusinessProfit(b.getBillCode());
						if (arg48 == null) {
							arg48 = new BusinessProfit();
							arg48.setBbCode(b.getBillCode());
							arg48.setId(idGenerator.getId(BusinessProfit.class
									.getName()));
							arg48.setCooperCost(new BigDecimal(0));
							arg48.setFeeCost(new BigDecimal(0));
							arg48.setMaterialCost(arg47);
							arg48.setProfitCost(b.getCost()
									.add(be.getAdjustCost())
									.subtract(arg48.getMaterialCost()));
							businessBillDao.save(arg48);
						} else {
							arg48.setMaterialCost(arg48.getMaterialCost().add(
									arg47));
							arg48.setProfitCost(arg48.getProfitCost().subtract(
									arg47));
							businessBillDao.update(arg48);
						}
					} else {
						outBillDao.save(arg40);

						for (arg44 = 0; arg44 < arg42.size(); ++arg44) {
							arg45 = (OutBillItem) arg42.get(arg44);
							outBillDao.save(arg45);
						}
					}
				}
			}
		}
	}

	public void txWcUnComplete(long billId, String itemIdStr, UserBean ub,
			BusinessBillDao businessBillDao,
			BusinessBillItemDao businessBillItemDao, OutBillDao outBillDao,
			OutBillItemDao outBillItemDao, StockDao stockDao,
			StockBalanceDao stockBalanceDao, IdGenerator idGenerator)
			throws Exception {
		BusinessBill b = (BusinessBill) businessBillItemDao.get(
				BusinessBill.class, billId);
		BusinessBillExt be = (BusinessBillExt) businessBillItemDao.get(
				BusinessBillExt.class, billId);
		if (b != null) {
			List itemList = businessBillItemDao.getListWidthExt(billId);
			String[] arr = itemIdStr.split(";");
			Date now = new Date();
			String completed = be.getCompleted();

			for (int obList = 0; obList < arr.length; ++obList) {
				long i = Long.parseLong(arr[obList]);

				for (int j = 0; j < itemList.size(); ++j) {
					Object[] objs = (Object[]) itemList.get(j);
					BusinessBillItem item = (BusinessBillItem) objs[0];
					BusinessBillItemExt ie = (BusinessBillItemExt) objs[1];
					if (item.getId() == i && item.getWcId() != 0L) {
						ie.setProFlag("B");
						ie.setWcComp("N");
						ie.setOperTime(now);
						ie.setOperId(ub.getStaffer().getId());
						ie.setOperName(ub.getStaffer().getStafferName());
						businessBillItemDao.update(ie);
						completed = "N";
						break;
					}
				}
			}

			be.setCompleted(completed);
			if (be.getCompleted().equals("N")) {
				be.setCompleteTime((Date) null);
			}

			businessBillItemDao.update(be);
			if (be.getCompleted().equals("N")) {
				List arg24 = outBillDao.getListByBusinessBill(b.getBillCode());

				for (int arg25 = 0; arg25 < arg24.size(); ++arg25) {
					OutBill ob = (OutBill) arg24.get(arg25);
					if (ob.getMark() != null
							&& ob.getMark().equals("业务单完工自动扣库存")) {
						PageHelper.cancelOutBill(ob, ub, "业务单反完工，自动作废相应的出库单",
								b, be.getAdjustCost(), "BUSI_UNCOMP",
								outBillItemDao, businessBillDao, stockDao,
								stockBalanceDao, idGenerator);
					}
				}
			}
		}

	}

	public void txCooUnComplete(long billId, String itemIdStr, UserBean ub,
			BusinessBillDao businessBillDao,
			BusinessBillItemDao businessBillItemDao, OutBillDao outBillDao,
			OutBillItemDao outBillItemDao, StockDao stockDao,
			StockBalanceDao stockBalanceDao, IdGenerator idGenerator)
			throws Exception {
		BusinessBill b = (BusinessBill) businessBillItemDao.get(
				BusinessBill.class, billId);
		BusinessBillExt be = (BusinessBillExt) businessBillItemDao.get(
				BusinessBillExt.class, billId);
		if (b != null) {
			List itemList = businessBillItemDao.getListWidthExt(billId);
			String[] arr = itemIdStr.split(";");
			Date now = new Date();
			String completed = be.getCompleted();

			for (int i = 0; i < arr.length; ++i) {
				long id = Long.parseLong(arr[i]);

				for (int j = 0; j < itemList.size(); ++j) {
					Object[] objs = (Object[]) itemList.get(j);
					BusinessBillItem item = (BusinessBillItem) objs[0];
					BusinessBillItemExt ie = (BusinessBillItemExt) objs[1];
					if (item.getId() == id && item.getIsCoop().equals("Y")) {
						ie.setCoopComp("N");
						ie.setOperTime(now);
						ie.setOperId(ub.getStaffer().getId());
						ie.setOperName(ub.getStaffer().getStafferName());
						businessBillItemDao.update(ie);
						completed = "N";
						break;
					}
				}
			}

			be.setCompleted(completed);
			if (be.getCompleted().equals("N")) {
				be.setCompleteTime((Date) null);
			}

			businessBillItemDao.update(be);
			be.getCompleted().equals("N");
		}

	}

	public Map txCooComplete(long billId, String itemIdStr, CodeRule obCr,
			List wcList, List mtList, UserBean ub, boolean noCompOthe,
			BusinessBillDao businessBillDao,
			BusinessBillItemDao businessBillItemDao, OutBillDao outBillDao,
			StockDao stockDao, StockBalanceDao stockBalanceDao,
			SerialNumberDao serialNumberDao, IdGenerator idGenerator)
			throws Exception {
		HashMap map = new HashMap();
		BusinessBill b = (BusinessBill) businessBillItemDao.get(
				BusinessBill.class, billId);
		BusinessBillExt be = (BusinessBillExt) businessBillItemDao.get(
				BusinessBillExt.class, billId);
		if (b != null) {
			if (noCompOthe
					&& (b.getFollowerIdStr() == null || b.getFollowerIdStr()
							.indexOf(";" + ub.getStaffer().getId() + ":") == -1)) {
				throw new NotAllowException("对不起，您没有权限完工其他人跟单的单据！");
			}

			String[] itemIdArr = (String[]) null;
			if (itemIdStr != null) {
				itemIdArr = itemIdStr.split(";");
			}

			Date now = new Date();
			List itemList = businessBillItemDao.getListWidthExt(billId);
			boolean itemChange = false;

			for (int completed = 0; completed < itemList.size(); ++completed) {
				Object[] msgContent = (Object[]) itemList.get(completed);
				BusinessBillItem i = (BusinessBillItem) msgContent[0];
				BusinessBillItemExt objs = (BusinessBillItemExt) msgContent[1];
				boolean item = false;
				if (itemIdArr != null) {
					for (int k = 0; k < itemIdArr.length; ++k) {
						if (i.getId() == Long.parseLong(itemIdArr[k])
								&& i.getIsCoop().equals("Y")
								&& objs.getCoopComp().equals("N")) {
							item = true;
							break;
						}
					}
				} else if (i.getIsCoop().equals("Y")
						&& objs.getCoopComp().equals("N")) {
					item = true;
				}

				if (item) {
					objs.setCoopComp("Y");
					objs.setOperTime(now);
					objs.setOperId(ub.getStaffer().getId());
					objs.setOperName(ub.getStaffer().getStafferName());
					businessBillItemDao.update(objs);
					itemChange = true;
				}
			}

			if (itemChange) {
				String arg28 = "Y";

				for (int arg29 = 0; arg29 < itemList.size(); ++arg29) {
					Object[] arg31 = (Object[]) itemList.get(arg29);
					BusinessBillItem arg33 = (BusinessBillItem) arg31[0];
					BusinessBillItemExt arg35 = (BusinessBillItemExt) arg31[1];
					if (arg33.getWcId() != 0L && arg35.getWcComp().equals("N")
							|| arg33.getIsCoop().equals("Y")
							&& arg35.getCoopComp().equals("N")) {
						arg28 = "N";
						break;
					}
				}

				be.setCompleted(arg28);
				if (be.getCompleted().equals("N")) {
					be.setCompleteTime((Date) null);
				} else {
					be.setCompleteTime(new Date());
				}

				businessBillItemDao.update(be);
				if (arg28.equals("Y")) {
					String arg30 = "";

					for (int arg32 = 0; arg32 < itemList.size(); ++arg32) {
						Object[] arg34 = (Object[]) itemList.get(arg32);
						BusinessBillItem arg36 = (BusinessBillItem) arg34[0];
						if (!arg30.equals("")) {
							arg30 = arg30 + "<br>";
						}

						arg30 = arg30
								+ PageHelper.createMsgContent(arg36, mtList);
					}

					map.put("comp", "Y");
					map.put("billId", Long.valueOf(b.getId()));
					map.put("billCode", b.getBillCode());
					map.put("estId", Long.valueOf(b.getClientId()));
					map.put("msgContent", arg30);
					map.put("follIdStr", b.getFollowerIdStr());
					map.put("follNameStr", b.getFollowerNameStr());
					map.put("ordeDate", b.getOrderDate());
					map.put("deliDate", b.getDeliverDate());
					map.put("phone", b.getPhone());
					map.put("linkman", b.getLinkman());
					map.put("content", b.getContent());
				}
			}
		}

		return map;
	}

	public Map txMake(long billId, long wcId, UserBean ub, List proList,
			List mtList, BusinessBillItemDao businessBillItemDao,
			IdGenerator idGenerator) throws Exception {
		HashMap map = new HashMap();
		String status = "OK";
		String msg = "";
		BusinessBill b = (BusinessBill) businessBillItemDao.get(
				BusinessBill.class, billId);
		BusinessBillExt be = (BusinessBillExt) businessBillItemDao.get(
				BusinessBillExt.class, billId);
		if (b != null) {
			if (be.getExamined().equals("N")) {
				msg = "业务单还没审核，不能开工!";
				status = "ERROR";
			} else if (be.getCompleted().equals("Y")) {
				msg = "业务单已经完工，不能开工!";
				status = "ERROR";
			} else if (be.getEnabled().equals("N")) {
				msg = "业务单已经作废，不能开工!";
				status = "ERROR";
			} else if (be.getClosed().equals("Y")) {
				msg = "业务单已经结清，不能开工!";
				status = "ERROR";
			} else {
				SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
				String msgContent = "";
				List itemList = businessBillItemDao.getListWidthExt(billId);
				Date now = new Date();

				for (int p = 0; p < itemList.size(); ++p) {
					Object[] objs = (Object[]) itemList.get(p);
					BusinessBillItem item = (BusinessBillItem) objs[0];
					BusinessBillItemExt ie = (BusinessBillItemExt) objs[1];
					if (item.getWcId() == wcId) {
						if (ie.getMakerId() != 0L) {
							throw new NotAllowException("业务单 "
									+ b.getBillCode() + " 中的项目已经被 "
									+ ie.getMakerName() + " 于 "
									+ sf.format(ie.getMakeTime()) + " 开工了！");
						}

						if (!msgContent.equals("")) {
							msgContent = msgContent + "<br>";
						}

						msgContent = msgContent
								+ PageHelper.createMsgContent(item, mtList);
						ie.setMaked("Y");
						ie.setMakerId(ub.getStaffer().getId());
						ie.setMakerName(ub.getStaffer().getStafferName());
						ie.setMakeTime(now);
						ie.setProId(this.getFirstProId(proList, wcId));
						ie.setProFlag("N");
						businessBillItemDao.update(ie);
					}
				}

				WorkProcess arg22 = this.getFirstPro(proList, wcId);
				if (arg22 != null && arg22.getDepId() != 0L) {
					map.put("makeMsg", "Y");
					map.put("billId", Long.valueOf(b.getId()));
					map.put("billCode", b.getBillCode());
					map.put("estId", Long.valueOf(b.getClientId()));
					map.put("depId", Long.valueOf(arg22.getDepId()));
					map.put("deliDate", b.getDeliverDate());
					map.put("msgContent", msgContent);
				}
			}
		}

		map.put("status", status);
		map.put("msg", msg);
		return map;
	}

	public String txUnMake(long billId, long wcId, UserBean ub,
			BusinessBillItemDao businessBillItemDao) throws Exception {
		String msg = "";
		BusinessBillExt be = (BusinessBillExt) businessBillItemDao.get(
				BusinessBillExt.class, billId);
		if (be != null) {
			if (be.getExamined().equals("N")) {
				msg = "业务单还没审核，不能取消开工!";
			} else if (be.getCompleted().equals("Y")) {
				msg = "业务单已经完工，不能取消开工!";
			} else if (be.getEnabled().equals("N")) {
				msg = "业务单已经作废，不能取消开工!";
			} else if (be.getClosed().equals("Y")) {
				msg = "业务单已经结清，不能取消开工!";
			} else {
				List itemList = businessBillItemDao.getListWidthExt(billId);

				for (int i = 0; i < itemList.size(); ++i) {
					Object[] objs = (Object[]) itemList.get(i);
					BusinessBillItem item = (BusinessBillItem) objs[0];
					BusinessBillItemExt ie = (BusinessBillItemExt) objs[1];
					if (item.getWcId() == wcId) {
						if (ub.getUser().getUserType() != 1
								&& ie.getMakerId() != ub.getStaffer().getId()) {
							throw new NotAllowException(
									"操作失败：只有系统管理员admin或开工人才能取消开工！");
						}

						ie.setMaked("N");
						ie.setMakerId(0L);
						ie.setMakerName("");
						ie.setMakeTime((Date) null);
						ie.setProId(0L);
						ie.setProFlag("N");
						businessBillItemDao.update(ie);
					}
				}

				msg = "保存成功！";
			}
		}

		return msg;
	}

	public String txUrgent(long billId, long wcId, String flag,
			BusinessBillItemDao businessBillItemDao) throws Exception {
		String msg = "";
		BusinessBillExt be = (BusinessBillExt) businessBillItemDao.get(
				BusinessBillExt.class, billId);
		if (be != null) {
			if (be.getExamined().equals("N")) {
				msg = "业务单还没审核，不能加急!";
			} else if (be.getCompleted().equals("Y")) {
				msg = "业务单已经完工，不能加急!";
			} else if (be.getEnabled().equals("N")) {
				msg = "业务单已经作废，不能加急!";
			} else if (be.getClosed().equals("Y")) {
				msg = "业务单已经结清，不能加急!";
			} else {
				List itemList = businessBillItemDao.getListWidthExt(billId);

				for (int i = 0; i < itemList.size(); ++i) {
					Object[] objs = (Object[]) itemList.get(i);
					BusinessBillItem item = (BusinessBillItem) objs[0];
					BusinessBillItemExt ie = (BusinessBillItemExt) objs[1];
					if (item.getWcId() == wcId) {
						ie.setUrgentFlag(flag);
						businessBillItemDao.update(ie);
					}
				}

				msg = "保存成功！";
			}
		}

		return msg;
	}

	public void txClose(long billId, String entBusiClos, long operId,
			String operName, BusinessBillDao businessBillDao,
			BusinessBillItemDao businessBillItemDao) throws Exception {
		Date now = new Date();
		BusinessBillExt be = (BusinessBillExt) businessBillItemDao.get(
				BusinessBillExt.class, billId);
		if (be != null && be.getEnabled().equals("Y")
				&& be.getExamined().equals("Y")
				&& be.getCollected().equals("Y") && be.getClosed().equals("N")) {
			be.setClosed("Y");
			be.setCloserId(operId);
			be.setCloserName(operName);
			be.setCloseTime(now);
			be.setExamSms("N");
			be.setMakeSms("N");
			boolean updaComp = false;
			if (entBusiClos.equals("AUTO_CD")) {
				if (be.getCompleted().equals("N")) {
					be.setCompleted("Y");
					be.setCompleteTime(now);
					updaComp = true;
				}

				if (be.getDelivered().equals("N")) {
					be.setDelivered("Y");
				}
			}

			businessBillItemDao.update(be);
			if (updaComp) {
				List itemList = businessBillItemDao.getListWidthExt(billId);

				for (int j = 0; j < itemList.size(); ++j) {
					Object[] objs = (Object[]) itemList.get(j);
					BusinessBillItem item = (BusinessBillItem) objs[0];
					BusinessBillItemExt ie = (BusinessBillItemExt) objs[1];
					boolean isUpd = false;
					if (item.getWcId() != 0L && ie.getWcComp().equals("N")) {
						ie.setWcComp("Y");
						ie.setOperId(operId);
						ie.setOperName(operName);
						ie.setOperTime(now);
						isUpd = true;
					}

					if (item.getIsCoop().equals("Y")
							&& ie.getCoopComp().equals("N")) {
						ie.setCoopComp("Y");
						ie.setOperId(operId);
						ie.setOperName(operName);
						ie.setOperTime(now);
						isUpd = true;
					}

					if (isUpd) {
						businessBillItemDao.update(ie);
					}
				}
			}
		}

	}

	public Map txProComplete(long billId, long itemId, long proId,
			List proList, List mtList, UserBean ub,
			BusinessBillItemDao businessBillItemDao, IdGenerator idGenerator) {
		BusinessBill b = (BusinessBill) businessBillItemDao.get(
				BusinessBill.class, billId);
		BusinessBillExt be = (BusinessBillExt) businessBillItemDao.get(
				BusinessBillExt.class, billId);
		HashMap map = new HashMap();
		String status = "OK";
		String msg = "";
		if (b != null) {
			if (be.getExamined().equals("N")) {
				msg = "业务单" + b.getBillCode() + "还没审核，不能操作!";
				status = "ERROR";
			} else if (be.getCompleted().equals("Y")) {
				msg = "业务单" + b.getBillCode() + "已经完工，不能操作!";
				status = "ERROR";
			} else if (be.getEnabled().equals("N")) {
				msg = "业务单" + b.getBillCode() + "已经作废，不能操作!";
				status = "ERROR";
			} else if (be.getClosed().equals("Y")) {
				msg = "业务单" + b.getBillCode() + "已经结清，不能操作!";
				status = "ERROR";
			} else {
				List itemList = businessBillItemDao.getListWidthExt(billId);
				BusinessBillItem thisItem = null;
				BusinessBillItemExt thisIe = null;

				for (int foundPro = 0; foundPro < itemList.size(); ++foundPro) {
					Object[] nextPro = (Object[]) itemList.get(foundPro);
					BusinessBillItem currentProName = (BusinessBillItem) nextPro[0];
					BusinessBillItemExt completed = (BusinessBillItemExt) nextPro[1];
					if (itemId == currentProName.getId()) {
						thisItem = currentProName;
						thisIe = completed;
						break;
					}
				}

				if (thisItem != null && thisIe.getProId() == proId) {
					if (thisIe.getMaked().equals("N")) {
						msg = b.getBillCode() + "的项目还没设置开工，不能操作!";
						status = "ERROR";
					} else if (thisIe.getWcComp().equals("Y")) {
						msg = b.getBillCode() + "的项目已经自制完工，不能操作!";
						status = "ERROR";
					} else {
						boolean arg27 = false;
						WorkProcess arg28 = null;
						String arg29 = "";

						for (int arg30 = 0; arg30 < proList.size(); ++arg30) {
							WorkProcess msgContent = (WorkProcess) proList
									.get(arg30);
							if (msgContent.getWorkCenterId() == thisItem
									.getWcId()) {
								if (arg27
										&& msgContent.getEnabled().equals("Y")) {
									arg28 = msgContent;
									break;
								}

								if (msgContent.getId() == proId) {
									arg27 = true;
									arg29 = msgContent.getProcessName();
								}
							}
						}

						if (arg28 != null) {
							thisIe.setProId(arg28.getId());
							thisIe.setProFlag("N");
						} else {
							Date arg31 = new Date();
							thisIe.setWcComp("Y");
							thisIe.setOperTime(arg31);
							thisIe.setOperId(ub.getStaffer().getId());
							thisIe.setOperName(ub.getStaffer().getStafferName());
						}

						businessBillItemDao.update(thisIe);
						String arg32 = "Y";

						for (int arg33 = 0; arg33 < itemList.size(); ++arg33) {
							Object[] i = (Object[]) itemList.get(arg33);
							BusinessBillItem objs = (BusinessBillItem) i[0];
							BusinessBillItemExt item = (BusinessBillItemExt) i[1];
							if (objs.getWcId() != 0L
									&& item.getWcComp().equals("N")
									|| objs.getIsCoop().equals("Y")
									&& item.getCoopComp().equals("N")) {
								arg32 = "N";
								break;
							}
						}

						be.setCompleted(arg32);
						if (be.getCompleted().equals("N")) {
							be.setCompleteTime((Date) null);
						} else {
							be.setCompleteTime(new Date());
						}

						businessBillItemDao.update(be);
						map.put("billId", Long.valueOf(b.getId()));
						map.put("billCode", b.getBillCode());
						map.put("estId", Long.valueOf(b.getClientId()));
						if (arg28 != null && arg28.getDepId() != 0L) {
							map.put("proMsg", "Y");
							map.put("depId", Long.valueOf(arg28.getDepId()));
							map.put("mtId",
									Long.valueOf(thisItem.getMakeTypeId()));
							map.put("currProName", arg29);
							map.put("msgContent", PageHelper.createMsgContent(
									thisItem, mtList));
						} else if (be.getCompleted().equals("Y")) {
							String arg34 = "";

							for (int arg35 = 0; arg35 < itemList.size(); ++arg35) {
								Object[] arg36 = (Object[]) itemList.get(arg35);
								BusinessBillItem arg37 = (BusinessBillItem) arg36[0];
								if (!arg34.equals("")) {
									arg34 = arg34 + "<br>";
								}

								arg34 = arg34
										+ PageHelper.createMsgContent(arg37,
												mtList);
							}

							map.put("comp", "Y");
							map.put("msgContent", arg34);
							map.put("follIdStr", b.getFollowerIdStr());
							map.put("follNameStr", b.getFollowerNameStr());
							map.put("ordeDate", b.getOrderDate());
							map.put("deliDate", b.getDeliverDate());
							map.put("phone", b.getPhone());
							map.put("linkman", b.getLinkman());
							map.put("content", b.getContent());
						}
					}
				}
			}
		}

		map.put("status", status);
		map.put("msg", msg);
		return map;
	}

	public Map txProBack(long billId, long itemId, long proId, List proList,
			List mtList, UserBean ub, BusinessBillItemDao businessBillItemDao,
			IdGenerator idGenerator) {
		HashMap map = new HashMap();
		String status = "OK";
		String msg = "";
		BusinessBill b = (BusinessBill) businessBillItemDao.get(
				BusinessBill.class, billId);
		BusinessBillExt be = (BusinessBillExt) businessBillItemDao.get(
				BusinessBillExt.class, billId);
		if (b != null) {
			if (be.getExamined().equals("N")) {
				msg = "业务单" + b.getBillCode() + "还没审核，不能操作!";
				status = "ERROR";
			} else if (be.getCompleted().equals("Y")) {
				msg = "业务单" + b.getBillCode() + "已经完工，不能操作!";
				status = "ERROR";
			} else if (be.getEnabled().equals("N")) {
				msg = "业务单" + b.getBillCode() + "已经作废，不能操作!";
				status = "ERROR";
			} else if (be.getClosed().equals("Y")) {
				msg = "业务单" + b.getBillCode() + "已经结清，不能操作!";
				status = "ERROR";
			} else {
				BusinessBillItem item = (BusinessBillItem) businessBillItemDao
						.get(BusinessBillItem.class, itemId);
				BusinessBillItemExt ie = (BusinessBillItemExt) businessBillItemDao
						.get(BusinessBillItemExt.class, itemId);
				if (item != null && ie.getProId() == proId) {
					if (ie.getMaked().equals("N")) {
						msg = b.getBillCode() + "的项目还没设置开工，不能操作!";
						status = "ERROR";
					} else if (ie.getWcComp().equals("Y")) {
						msg = b.getBillCode() + "的项目已经自制完工，不能操作!";
						status = "ERROR";
					} else {
						WorkProcess prePro = null;
						String currentProName = "";

						for (int i = 0; i < proList.size(); ++i) {
							WorkProcess p = (WorkProcess) proList.get(i);
							if (p.getWorkCenterId() == item.getWcId()) {
								if (p.getId() != proId
										&& p.getEnabled().equals("Y")) {
									prePro = p;
								}

								if (p.getId() == proId) {
									currentProName = p.getProcessName();
									break;
								}
							}
						}

						if (prePro != null) {
							ie.setProId(prePro.getId());
							ie.setProFlag("B");
							businessBillItemDao.update(ie);
							if (prePro.getDepId() != 0L) {
								map.put("proMsg", "Y");
								map.put("billId", Long.valueOf(b.getId()));
								map.put("billCode", b.getBillCode());
								map.put("estId", Long.valueOf(b.getClientId()));
								map.put("depId",
										Long.valueOf(prePro.getDepId()));
								map.put("mtId",
										Long.valueOf(item.getMakeTypeId()));
								map.put("currProName", currentProName);
								map.put("msgContent", PageHelper
										.createMsgContent(item, mtList));
							}
						}
					}
				}
			}
		}

		map.put("status", status);
		map.put("msg", msg);
		return map;
	}

	public Map txProSet(long billId, long itemId, long wcId, long proId,
			List mtList, UserBean ub, BusinessBillItemDao businessBillItemDao,
			IdGenerator idGenerator) {
		HashMap map = new HashMap();
		String status = "OK";
		String msg = "";
		BusinessBill b = (BusinessBill) businessBillItemDao.get(
				BusinessBill.class, billId);
		BusinessBillExt be = (BusinessBillExt) businessBillItemDao.get(
				BusinessBillExt.class, billId);
		if (b != null) {
			if (be.getExamined().equals("N")) {
				msg = "业务单" + b.getBillCode() + "还没审核，不能操作!";
				status = "ERROR";
			} else if (be.getCompleted().equals("Y")) {
				msg = "业务单" + b.getBillCode() + "已经完工，不能操作!";
				status = "ERROR";
			} else if (be.getEnabled().equals("N")) {
				msg = "业务单" + b.getBillCode() + "已经作废，不能操作!";
				status = "ERROR";
			} else if (be.getClosed().equals("Y")) {
				msg = "业务单" + b.getBillCode() + "已经结清，不能操作!";
				status = "ERROR";
			} else {
				BusinessBillItem item = (BusinessBillItem) businessBillItemDao
						.get(BusinessBillItem.class, itemId);
				BusinessBillItemExt ie = (BusinessBillItemExt) businessBillItemDao
						.get(BusinessBillItemExt.class, itemId);
				if (item != null && item.getWcId() == wcId) {
					if (ie.getMaked().equals("N")) {
						msg = b.getBillCode() + "的项目还没设置开工，不能操作!";
						status = "ERROR";
					} else if (ie.getWcComp().equals("Y")) {
						msg = b.getBillCode() + "的项目已经自制完工，不能操作!";
						status = "ERROR";
					} else {
						long preId = ie.getProId();
						ie.setProId(proId);
						ie.setProFlag("N");
						businessBillItemDao.update(ie);
						WorkProcess curPro = (WorkProcess) businessBillItemDao
								.get(WorkProcess.class, proId);
						if (curPro != null && curPro.getDepId() != 0L) {
							String preName = "";
							WorkProcess prePro = (WorkProcess) businessBillItemDao
									.get(WorkProcess.class, preId);
							if (prePro != null) {
								preName = prePro.getProcessName();
							}

							map.put("proMsg", "Y");
							map.put("billId", Long.valueOf(b.getId()));
							map.put("billCode", b.getBillCode());
							map.put("estId", Long.valueOf(b.getClientId()));
							map.put("depId", Long.valueOf(curPro.getDepId()));
							map.put("mtId", Long.valueOf(item.getMakeTypeId()));
							map.put("prevProName", curPro.getProcessName());
							map.put("currProName", preName);
							map.put("msgContent",
									PageHelper.createMsgContent(item, mtList));
						}
					}
				}
			}
		}

		map.put("status", status);
		map.put("msg", msg);
		return map;
	}
}